package problem525;

//525.连续数组
//https://leetcode.cn/problems/contiguous-array/

import java.util.Arrays;
import java.util.HashMap;

class Solution {
    public int findMaxLength(int[] nums) {
        int n = nums.length;
        for(int i = 0; i<n; i++) {
            nums[i] = nums[i] == 0 ? -1 : 1;
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, -1);
        int sum = 0, ret = 0;
        for(int i = 0; i<n; i++) {
            sum += nums[i];
            if(map.containsKey(sum)) ret = Math.max(ret, i-map.get(sum));
            else map.put(sum, i);
        }
        return ret;
    }
}